package com.atguigu.day03;

import com.atguigu.utils.ClickEvent;
import com.atguigu.utils.ClickSource;
import org.apache.flink.api.common.state.MapState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

public class Example2 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env
                .addSource(new ClickSource())
                .keyBy(r -> r.username)
                .process(new KeyedProcessFunction<String, ClickEvent, String>() {
                    private MapState<String, Integer> urlCount;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        urlCount = getRuntimeContext().getMapState(
                                new MapStateDescriptor<String, Integer>(
                                        "url-count",
                                        Types.STRING, // key的类型
                                        Types.INT     // value的类型
                                )
                        );
                    }

                    @Override
                    public void processElement(ClickEvent value, Context ctx, Collector<String> out) throws Exception {
                        if (!urlCount.contains(value.url)) {
                            urlCount.put(value.url, 1);
                        } else {
                            urlCount.put(value.url, urlCount.get(value.url) + 1);
                        }

                        // 格式化输出结果
                        StringBuilder result = new StringBuilder();
                        result.append(value.username + " => {\n");
                        for (String url : urlCount.keys()) {
                            result.append("  " + url + " -> " + urlCount.get(url) + ",\n");
                        }
                        result.append("}\n");
                        out.collect(result.toString());
                    }
                })
                .print();

        env.execute();
    }
}
